/**
 *@author  jessuLv
 *@com     runcom
 *@version V1.0
 *@date    Jul 31, 2008 8:31:38 AM
 */
package cn.edu.zucc.leyi.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class StringHelper {

	/**
	 * Wrap the every element in elements parameter with the wrapper parameter. <br>
	 * elements: [a,b,c]. <br>
	 * wrapper: '''. <br>
	 * result: ['a','b','c'].
	 * 
	 * @param wrapper
	 * @param elements
	 * @return
	 */
	public static String[] wrapper(String wrapper, String[] elements) {
		if (elements == null) {
			return null;
		}
		for (int i = 0; i < elements.length; i++) {
			elements[i] = wrapper + elements[i] + wrapper;
		}
		return elements;
	}

	public static String join(String seperator, String[] elements) {
		if (elements == null) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		if (elements.length > 0) {
			sb.append(elements[0]);
		}
		for (int i = 1; i < elements.length; i++) {
			sb.append(seperator).append(elements[i]);
		}
		return sb.toString();
	}

	public static String join(String seperator, List<String> elements) {
		if (elements == null) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		if (elements.size() > 0) {
			sb.append(elements.get(0));
		}
		for (int i = 1; i < elements.size(); i++) {
			sb.append(seperator).append(elements.get(i));
		}
		return sb.toString();
	}

	public static List<String> join(String seperator,
			Map<String, String> elements) {
		if (elements == null) {
			return null;
		}
		List<String> list = new ArrayList<String>();
		Iterator<Map.Entry<String, String>> itr = elements.entrySet()
				.iterator();
		while (itr.hasNext()) {
			Map.Entry<String, String> entry = itr.next();
			list.add(entry.getKey() + seperator + entry.getValue());
		}
		return list;
	}

}
